package com.ryh.shiro.realm;

import com.ryh.entity.Permission;
import com.ryh.entity.Role;
import com.ryh.entity.User;
import com.ryh.service.PermissionService;
import com.ryh.service.RoleService;
import com.ryh.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @ClassName MyRealm
 * @Description 自定义加密realm
 * @Author RaoYH
 */
public class MyRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //1、从主体中传过来的认证信息中，获取用户名
        String userName = (String) principals.getPrimaryPrincipal();
        //2、通过用户名查询数据库或者缓存中获取凭证
        User user = userService.findByName(userName);
        Set<String> roles = new HashSet<>();
        Set<String> permissions = new HashSet<>();
        if (null != user) {
            List<Role> roleList = roleService.findById(user.getRid());
            Set<Integer> permissionIds = new HashSet<>();
            for (Role role : roleList) {
                permissionIds.add(role.getId());
                roles.add(role.getName());
            }
            List<Permission> permissionList = permissionService.findByIds(permissionIds);
            for (Permission permission : permissionList) {
                permissions.add(permission.getName());
            }
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            info.setRoles(roles);
            info.setStringPermissions(permissions);
            return info;

        }

        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //1、从主体中传过来的认证信息中，获取用户名
        String userName = (String) token.getPrincipal();
        //2、通过用户名查询数据库或者缓存中获取凭证
        User user = userService.findByName(userName);
        if (user != null) {

            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getUserName(), user.getPassword(), "MyRealm");

            //再返回之前将盐设置进去
            info.setCredentialsSalt(ByteSource.Util.bytes(user.getUserName()));
            return info;
        }
        return null;
    }

    public static void main(String[] args) {
        //获取md5加密后的密文
        Md5Hash md5Hash = new Md5Hash("362524");
        System.out.println(md5Hash);
        //获取md5 + 盐 加密后的密文

        Md5Hash md5Hash1 = new Md5Hash("362524", "rao");
        System.out.println(md5Hash1);
    }
}
